import os

import pytest
from errors import Duplicate, Missing
from model.user import User

# set this before data.init import below
os.environ["CRYPTID_SQLITE_DB"] = ":memory:"
from data import user


@pytest.fixture
def sample() -> User:
    return User(name="hazadus", hash="12345678")


def test_create(sample):
    resp = user.create(sample)
    assert resp == sample


def test_create_duplicate(sample):
    with pytest.raises(Duplicate):
        user.create(sample)


def test_get_one(sample):
    resp = user.get_one(sample.name)
    assert resp == sample


def test_get_one_missing():
    with pytest.raises(Missing):
        user.get_one("mfdoom")


def test_modify(sample):
    modified = User(
        name=sample.name,
        hash=sample.hash,
    )
    user.hash = "1234"
    resp = user.modify(sample.name, modified)
    assert resp == modified


def test_modify_missing():
    thing: User = User(name="mastakilla", hash="890")
    with pytest.raises(Missing):
        user.modify(thing.name, thing)


def test_delete(sample):
    resp = user.delete(sample.name)
    assert resp is None


def test_delete_missing(sample):
    with pytest.raises(Missing):
        user.delete(sample.name)
